package com.example.j202411.views.Activity;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;

import com.example.j202411.R;
import com.example.j202411.adapter.MovieAdapter;
import com.example.j202411.Model.Movie;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MovieActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private MovieAdapter adapter;
    private List<Movie> movieList;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        movieList = new ArrayList<>();

        fetchData();
    }

    private void fetchData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("https://api.lolimi.cn/API/piao/dy.php")
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                Log.e("OkHttp", "Request Failed", e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String responseData = response.body().string();
                parseData(responseData);
            }
        });
    }

    private void parseData(String text) {
        String[] lines = text.split("\n");
        for (String line : lines) {
            if (line.matches("\\d+\\. .+")) { // 匹配数字和电影名的行
                line = line.replaceFirst("\\d+\\. ", ""); // 去除行首的序号和点号

                // 使用正则表达式匹配电影名、上映天数和票房
                Pattern pattern = Pattern.compile("(.+)上映(\\d+)天(.+)");
                Matcher matcher = pattern.matcher(line);

                if (matcher.find()) {
                    String movieName = matcher.group(1).trim();
                    String releaseDays = matcher.group(2).trim();
                    String boxOffice = matcher.group(3).trim();

                    Movie movie = new Movie();
                    movie.setIndex(line.split("\\.")[0].trim());
                    movie.setTitle(movieName);
                    movie.setReleaseInfo("上映" + releaseDays + "天");
                    movie.setSumBoxDese(boxOffice);
                    movieList.add(movie);
                } else {
                    Log.e("MainActivity", "Movie name and release info format error: " + line);
                }
            }
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter = new MovieAdapter(movieList);
                recyclerView.setAdapter(adapter);
            }
        });
    }
}